const commonUtils = require('../common/commonUtils');
const {request, showTip, getCurrentUserInfo} = require("../common/request");
//动态计算当前页面的可滚动的高度
function getScrollHeight(){
    // 计算滚动区域的高度：屏幕高度 - 导航栏高度 - 底部安全区域高度
    return  wx.getSystemInfoSync().windowHeight - 240;
}
//初始化数据
function initInfo(_this) {
    getCoursePointList(_this);
}

// pages/yueke/yueke.js
Page({
    data: {
        scrollHeight:getScrollHeight(),
        currentTab: '0',//选项卡默认
        coursePointList:{},//预约课程列表
    },
    onLoad(options) {
        initInfo(this);
    },
    changePointTab(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
            currentTab: index
        });
        getCoursePointList(this);
    },
    // 下拉刷新触发时执行
    onPullDownRefresh() {
        wx.showLoading({ title: '刷新中...' });
        initInfo(this);
        wx.stopPullDownRefresh();// 停止刷新动画
        wx.hideLoading();
    },
    //取消
    canclPoint:function(e){
        const that = this;
        const id = e.currentTarget.dataset.id;
        //弹窗提示是否确认预约
        const userInfo = getCurrentUserInfo();
        wx.showModal({
            title: '确认提醒',
            content: '是否确认执行该操作？',
            confirmText: '是',
            cancelText: '否',
            success: (res) => {
                if (res.confirm) {
                    // 点击确认按钮后的处理逻辑
                    request({
                        url:'/yoga_business/wechat/coursePlanPoint/cancel',
                        method:'POST',
                        data:{
                            "openId":userInfo.openId,
                            "id":id
                        },
                        success(res) {
                            if(res.success){
                                initInfo(that);
                            }else{
                                showTip();
                            }
                        },
                        fail(msg) {
                            showTip();
                        }
                    });
                }
            }
        });
    },
})

/**
 * 获取已预约的课程列表
 * @param _this
 * @param currentTab
 */
const getCoursePointList = (_this) => {
    const userInfo = getCurrentUserInfo();
    request({
        url:'/yoga_business/wechat/coursePlanPoint/list',
        method:'POST',
        data:{
            "openId":userInfo.openId,
            "status":_this.data.currentTab
        },
        success(res) {
            if(res.success){
                _this.setData({coursePointList:res.content});
            }else{
                showTip();
            }
        },
        fail(msg) {
            showTip();
        }
    });
};